package com.example.demo.controller;

import com.example.demo.model.*;
import com.example.demo.service.OlderService;
import com.example.demo.tools.Constant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: WJ
 * Date: 2023-02-17
 * Time: 21:36
 */
@RestController
@RequestMapping("/older")
public class OlderController {

    @Autowired
    private OlderService olderService;


    //    获取所有的老人信息
    @RequestMapping("/getallolderdetailinfobyall")
    public Object getAllOlderDetailInfoByAll(){
        return olderService.getAllOlderDetailInfo(null);
    }


//    获取所有的老人信息(某一个admin下的)
    @RequestMapping("/getallolderdetailinfo")
    public Object getAllOlderDetailInfo(){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return olderService.getAllOlderDetailInfo(user.getId());
    }

//    在后台的老人管理中更新老人信息
    @RequestMapping("/updateolderinfoinadmin")
    public Object updateOlderInfoInAdmin(@RequestParam Integer id,
                                         @RequestParam String address,
                                         @RequestParam Integer age,
                                         @RequestParam String id_card,
                                         @RequestParam String clan,
                                         @RequestParam String clan_phone,
                                         @RequestParam Integer employee_id,
                                         @RequestParam String other_info,
                                         @RequestParam String phone,
                                         @RequestParam String sex,
                                         @RequestParam String username){

        return olderService.updateOlderInfoInAdmin(id,address,age,clan,clan_phone,employee_id,other_info,phone,sex,username,id_card,null,null,null);
    }

    //    在前台中更新老人信息
    @RequestMapping("/updateolderinfoinuser")
    public Object updateOlderInfoInUser(@RequestParam  Integer id,
                                        @RequestParam String address,
                                        @RequestParam Integer age,
                                        @RequestParam String id_card,
                                        @RequestParam String clan,
                                        @RequestParam String clan_phone,
                                        @RequestParam String other_info,
                                        @RequestParam String phone,
                                        @RequestParam String sex,
                                        @RequestParam String username,
                                        @RequestParam String photo_url,
                                        @RequestParam String password,
                                        @RequestParam String isLogin){

        return olderService.updateOlderInfoInAdmin(id,address,age,clan,clan_phone,null,other_info,phone,sex,username,id_card,photo_url,password,isLogin);
    }

//    删除老人
    @RequestMapping("/deleteolderbyid")
    public Object deleteOlderById(@RequestParam Integer id){
        return olderService.deleteOlderById(id);
    }

//    获取所有的老人健康档案
    @RequestMapping("/getallolderhealthfiles")
    public Object getAllOlderHealthFiles(){
        return olderService.getAllOlderHealthFiles();
    }

//    通过healthId删除对应健康档案
    @RequestMapping("/deletehealthfile")
    public Object deleteHealthFileById(@RequestParam Integer id){
        return olderService.deleteHealthFileById(id);
    }

//    修改健康档案
    @RequestMapping("/updatehealthfilebyid")
    public Object updateHealthFileById(@RequestParam Integer id,
                                       @RequestParam Integer employee_id,
                                       @RequestParam Integer vaccination,
                                       @RequestParam Float temperature,
                                       @RequestParam Integer height,
                                       @RequestParam Float   weight,
                                       @RequestParam Integer min_pressure,
                                       @RequestParam Integer respiratory_rate,
                                       @RequestParam Integer pulse,
                                       @RequestParam String  electrocardiogram,
                                       @RequestParam Integer high_pressure){

        return olderService.updateHealthFileById(id,employee_id,vaccination,temperature,height,weight,min_pressure,respiratory_rate,pulse,electrocardiogram,high_pressure);
    }

//    根据条件查询老人信息（老人模块的查询,可以模糊查询）
    @RequestMapping("/getallolderbylike")
    public Object getAllOlderInfoByUsernameOrAgeD(String username,
                                                  String phone,
                                                  String id_card,
                                                  Integer age){
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        TokenUser principal = (TokenUser) authentication.getPrincipal();
        LoginUser user = principal.getUser();
        return olderService.getAllOlderInfoByUsernameOrAgeD(user.getId(),username,phone,id_card,age);
    }

//    根据条件查询老人健康档案信息（档案模块的查询,可以模糊查询）
    @RequestMapping("/getallolderhealthbyrange")
    public Object getAllOlderHealthInfoByMinPreOrMaxPre(Integer minMinPre,
                                                        Integer minMaxPre,
                                                        Integer maxMinPre,
                                                        Integer maxMaxPre){

        return olderService.getAllOlderHealthInfoByMinPreOrMaxPre(minMinPre,minMaxPre,maxMinPre,maxMaxPre);
    }

//    批量删除老人信息
    @RequestMapping("/deleteonegroupolder")
    public Object deleteOneGroupOlderInfo(IDS ids){
        if(ids == null || ids.getOldersID() == null){
            return "批量删除参数接收失败!";
        }
        return olderService.deleteOneGroupOlderInfo(ids.getOldersID());
    }

//    批量删除老人健康档案信息
    @RequestMapping("/deleteonegroupolderhealthinfo")
    public Object deleteOneGroupOlderHealthInfo(IDS ids){
        if(ids == null || ids.getOldersID() == null){
            return "批量删除参数接收失败!";
        }
        return olderService.deleteOneGroupOlderHealthInfo(ids.getOldersID());
    }

//    添加老人
    @RequestMapping("/addolderinfo")
    public Object addOlderInOlderManage(String photo_url,
                                        String other,
                                        @RequestParam String username,
                                        @RequestParam String sex,
                                        @RequestParam Integer age,
                                        @RequestParam String address,
                                        @RequestParam String phone,
                                        @RequestParam String id_card,
                                        @RequestParam Integer employee_id,
                                        @RequestParam String clan,
                                        @RequestParam String clan_phone){

        // 将接收到的参数封装成一个对象
        Older older = new Older();
        older.setUsername(username);
        older.setSex(sex);
        older.setAge(age);
        older.setAddress(address);
        older.setPhone(phone);
        older.setId_card(id_card);
        older.setEmployee_id(employee_id);
        older.setClan(clan);
        older.setClan_phone(clan_phone);

        return olderService.addOlderInOlderManage(older,photo_url,other);
    }

//    通过老人id获取所有的健康档案
    @RequestMapping("/getallhealthfilesbyolderid")
    public Object getAllHealthFilesByOlderId(@RequestParam Integer id){
        return olderService.getAllHealthFilesByOlderId(id);
    }

//    通过老人id获取该老人的详细信息
    @RequestMapping("/getallinfobyolderid")
    public Object getInfoByOlderId(Integer id){
        return olderService.getInfoByOlderId(id);
    }

//    通过老人id获取老人报名的活动信息
    @RequestMapping("/getallactivebyolderid")
    public Object getAllActiveByOlderId(@RequestParam Integer id){
        return olderService.getAllActiveByOlderId(id);
    }

//    检查是否评价过该员工
    @RequestMapping("/checkisappraise")
    public Object checkIsAppraise(@RequestParam(value = "employeeid") Integer employeeId,
                                  @RequestParam(value = "olderid") Integer olderId){

        return olderService.checkIsAppraise(employeeId,olderId);
    }

//    老人用户打分
    @RequestMapping("/commentgrade")
    public Object commentGrade(@RequestParam Integer id,
                               @RequestParam Float value,
                               @RequestParam String txt,
                               @RequestParam Integer older_id){

        return olderService.commentGrade(id,value,txt,older_id);
    }

//    前台页面查询活动信息(根据条件可以模糊查询)
    @RequestMapping("/getactivebyitemsuser")
    public Object getActiveByItems(String active_name,
                                   String active_address,
                                   Integer active_id,
                                   ArrayTimeList timeList){

        return olderService.getActiveByItems(active_id,active_name,active_address,timeList.getDate());
    }

//    老人报名参加活动
    @RequestMapping("/joinactiveonolder")
    public Object joinActiveForOlder(@RequestParam Integer active_id,
                                     @RequestParam Integer older_id,
                                     @RequestParam String active_name,
                                     @RequestParam String older_name){

        return olderService.joinActiveForOlder(active_id,active_name,older_id,older_name);
    }

//    添加老人健康档案
    @RequestMapping("/addolderhealthfile")
    public Object addOlderHealthFileInfo(@RequestParam Integer older_id,
                                         @RequestParam Float temperature,
                                         @RequestParam Integer height,
                                         @RequestParam Float weight,
                                         @RequestParam Integer respiratory_rate,
                                         @RequestParam Integer pulse,
                                         @RequestParam Integer min_pressure,
                                         @RequestParam Integer high_pressure,
                                         @RequestParam String electrocardiogram,
                                         @RequestParam Integer vaccination,
                                         @RequestParam Integer employee_id){

        return olderService.addOlderHealthFileInfo(older_id,temperature,height,weight,respiratory_rate,pulse,min_pressure,high_pressure,electrocardiogram,vaccination,employee_id);
    }

//    获取老人表明活动的信息列表
    @RequestMapping("/getjoinactiveonolder")
    public Object getJoinActiveOnOlder(@RequestParam Integer id){
        return olderService.getJoinActiveOnOlder(id);
    }

//    通过活动id获取所有的评论信息
    @RequestMapping("/getallcommentbyactiveid")
    public Object getAllCommentByActiveId(@RequestParam Integer id){
        return olderService.getAllCommentByActiveId(id);
    }
}
